From 78b93e2c04ad92e52fe0b7e9ef19caacb55c95a6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C3=98yvind=20Kol=C3=A5s?= Date: Sat, 21 Nov 2009 15:55:11 +0000 Subject: [PATCH] Added a BablMutex type. Mutex abstraction for internal use pthreads by default and critical sections on win32. --- babl/Makefile.am | 2 ++ babl/babl-internal.h | 1 + babl/babl-mutex.c | 67 ++++++++++++++++++++++++++++++++++++++++++++ babl/babl-mutex.h | 40 ++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 babl/babl-mutex.c create mode 100644 babl/babl-mutex.h diff --git a/babl/Makefile.am b/babl/Makefile.am index bc5b9ec..d62125c 100644 --- a/babl/Makefile.am +++ b/babl/Makefile.am @@ -27,6 +27,7 @@ c_sources = \ babl-list.c \ babl-memory.c \ babl-model.c \ + babl-mutex.c \ babl-sampling.c \ babl-sanity.c \ babl-type.c \ @@ -53,6 +54,7 @@ h_sources = \ babl-macros.h \ babl-memory.h \ babl-model.h \ + babl-mutex.h \ babl-sampling.h \ babl-type.h \ babl-types.h \ diff --git a/babl/babl-internal.h b/babl/babl-internal.h index 1323c12..bb71c2d 100644 --- a/babl/babl-internal.h +++ b/babl/babl-internal.h @@ -49,6 +49,7 @@ #include "babl-ids.h" #include "babl-util.h" #include "babl-memory.h" +#include "babl-mutex.h" #include "babl-cpuaccel.h" /* redefining some functions for the win32 platform */ diff --git a/babl/babl-mutex.c b/babl/babl-mutex.c new file mode 100644 index 0000000..99c8615 --- /dev/null +++ b/babl/babl-mutex.c @@ -0,0 +1,67 @@ +/* babl - dynamically extendable universal pixel conversion library. + * Copyright (C) 2009, Øyvind Kolås. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, see + * . + */ + +#include "config.h" +#include +#include +#include +#include +#include "babl-mutex.h" + +BablMutex * +babl_mutex_new (void) +{ + BablMutex *mutex = malloc (sizeof (BablMutex)); +#ifdef WIN32 + InitializeCriticalSection (mutex); +#else + pthread_mutex_init (mutex, NULL); +#endif + return mutex; +} + +void +babl_mutex_destroy (BablMutex *mutex) +{ +#ifdef WIN32 + DeleteCriticalSection (mutex); +#else + pthread_mutex_destroy(mutex); +#endif + free (mutex); +} + +void +babl_mutex_lock (BablMutex *mutex) +{ +#ifdef WIN32 + EnterCriticalSection (mutex); +#else + pthread_mutex_lock (mutex); +#endif +} + +void +babl_mutex_unlock (BablMutex *mutex) +{ +#ifdef WIN32 + LeaveCriticalSection (mutex); +#else + pthread_mutex_unlock (mutex); +#endif +} diff --git a/babl/babl-mutex.h b/babl/babl-mutex.h new file mode 100644 index 0000000..a73e480 --- /dev/null +++ b/babl/babl-mutex.h @@ -0,0 +1,40 @@ +/* babl - dynamically extendable universal pixel conversion library. + * Copyright (C) 2009, Øyvind Kolås. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, see + * . + */ + +#ifndef _BABL_MUTEX_H +#define _BABL_MUTEX_H + +#ifndef WIN32 +#define __USE_GNU 1 +#include +#else +#include +#endif + +#ifdef WIN32 + typedef CriticalSection BablMutex; +#else + typedef pthread_mutex_t BablMutex; +#endif + +BablMutex* babl_mutex_new (void); +void babl_mutex_destroy (BablMutex *mutex); +void babl_mutex_lock (BablMutex *mutex); +void babl_mutex_unlock (BablMutex *mutex); + +#endif -- 2.30.2